package linkList;

import linkList.reverseList.ListNode;
import utils.linkListUtil;

public class reverseList {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    // 迭代法
    public static ListNode reverseList(ListNode head) {
        // 为空或者只有一个元素
        if (head == null || head.next == null) {
            return head;
        }
        ListNode pre=null,temp=null;
        while (head!=null) {
            temp=head.next;
            head.next=pre;
            pre=head;
            head=temp;
        }
        return pre;
    }

    public static void main(String[] args) {
         int[] arrA = {4, 1, 8, 4, 5};
         
        // ListNode headA = (ListNode) linkListUtil.createLinkedList(arrA);
        
        // ListNode reverseList = reverseList(headA);
        // System.out.println(reverseList);

    }
}
